(function (vc, vm) {
    vc.extends({
        data: {
            editShopInfo: {
                shopId: '',
                shopName: '',
                shopLogo: '2',
                shopType: '',
                openType: '',
                shopDesc: '',
                changeLogo: "",
                sendAddress: '',
                storeId: '',
                returnPerson: '',
                returnLink: '',
                returnAddress: '',
                shopRange: '',
                shopRangeSel: [],
                shopRanges: [],
                openTypes: [],
                applyOpinion: '',
                mapX: '',
                mapY: '',
                state: '',
                areaCode: '',
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('editShop', 'openEditShopModal', function (_params) {
                $that.refreshEditShopInfo();
                $('#editShopModel').modal('show');
                vc.getDict('s_shop', "open_type", function (_data) {
                    $that.editShopInfo.openTypes = _data;
                });
                vc.copyObject(_params, $that.editShopInfo);
                
                let _photos = [];
                _photos.push($that.editShopInfo.shopLogo);
                vc.emit('editIconCover', 'uploadImageUrl', 'notifyPhotos', _photos);
                var map = new AMap.Map("containeredit", {
                    zoom: 14, //初始化地图层级
                    center: [$that.editShopInfo.mapX, $that.editShopInfo.mapY],
                    resizeEnable: true
                });
                // 同时引入工具条插件，比例尺插件和鹰眼插件
                AMap.plugin([
                    'AMap.ToolBar',
                    'AMap.Geolocation',
                ], function () {
                    // 在图面添加工具条控件，工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
                    map.addControl(new AMap.ToolBar());
                    // 在图面添加定位控件，用来获取和展示用户主机所在的经纬度位置
                    map.addControl(new AMap.Geolocation());
                });
                //为地图注册click事件获取鼠标点击出的经纬度坐标
                map.on('click', function (e) {
                    vc.toast("成功获取坐标。");
                    $that.editShopInfo.mapX = e.lnglat.getLng()
                    $that.editShopInfo.mapY = e.lnglat.getLat()
                });
            });
            vc.on("editShop", "notifyUploadCoverImage", function (_param) {
                if (_param.length > 0) {
                    $that.editShopInfo.shopLogo = _param[_param.length - 1].url;
                } else {
                    $that.editShopInfo.shopLogo = '';
                }
            });
        },
        methods: {
            editShopValidate: function () {
                return vc.validate.validate({
                    editShopInfo: $that.editShopInfo
                }, {
                    'editShopInfo.shopName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "128",
                            errInfo: "商铺名称太长"
                        },
                    ],
                    'editShopInfo.shopLogo': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺logo不能为空"
                        }
                    ],
                    'editShopInfo.shopType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺类型不能为空"
                        }
                    ],
                    'editShopInfo.openType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "开店类型不能为空"
                        }
                    ],
                    'editShopInfo.shopDesc': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺描述不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "商铺描述太长"
                        },
                    ],
                    'editShopInfo.sendAddress': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "发货地址不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "发货地址太长"
                        },
                    ],
                    'editShopInfo.returnPerson': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "退货联系人不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "退货联系人太长"
                        },
                    ],
                    'editShopInfo.returnLink': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "退货联系电话不能为空"
                        }
                    ],
                    'editShopInfo.mapX': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "坐标X位置不能为空"
                        }
                    ],
                    'editShopInfo.mapY': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "坐标Y位置不能为空"
                        }
                    ],
                    'editShopInfo.returnAddress': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "退货地址不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "退货地址太长"
                        },
                    ],
                    'editShopInfo.shopId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺编号不能为空"
                        }
                    ],
                });
            },
            editShop: function () {
                if (!$that.editShopValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }

                vc.http.apiPost(
                    '/shop.updateShop',
                    JSON.stringify($that.editShopInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editShopModel').modal('hide');
                            vc.emit('shop', 'listShop', {});
                            vc.toast(_json.msg);
                            return;
                        }
                    },
                    function (errInfo, error) {
                        vc.toast(errInfo);
                    });

            },
            refreshEditShopInfo: function () {
                vc.emit('uploadImageUrl', 'clearImage', {});
                $that.editShopInfo = {
                    shopId: '',
                    shopName: '',
                    applyOpinion: '',
                    shopLogo: '2',
                    storeId: '',
                    changeLogo: '',
                    shopType: '',
                    openType: '',
                    shopDesc: '',
                    sendAddress: '',
                    returnPerson: '',
                    returnLink: '',
                    returnAddress: '',
                    shopRange: '',
                    shopRangeSel: [],
                    shopRanges: [],
                    openTypes: [],
                    mapX: '',
                    mapY: '',
                    state: '',
                    areaCode: '',
                }
            },
            _changeEditShopType: function () {
                $that.editShopInfo.shopRangeSel = [];
            },
        }
    });
})(window.vc, window.$that);
